package com.JavaTest8;

import java.io.*;
//import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Scanner;

public class CarTest {
    static Scanner in = new Scanner(System.in);

    public static void main(String[] args) {
        ArrayList<Car> cs = new ArrayList<Car>();
        ArrayList<CarManagement> cm = new ArrayList<CarManagement>();
        System.out.println("-------生产情况--------");
        produceCar(cs);
        System.out.println("-------销售情况--------");
        sellCar(cs, cm);
        System.out.println("-------所有车辆情况--------");
        listCar(cs);
        System.out.println("-------所有销售情况--------");
        listSoldCar(cm);
        System.out.println("-------删除销售数据--------");
        deleteSoldCar(cm);
        System.out.println("-------改动后的销售情况-------");
        listSoldCar(cm);
    }

    public static void produceCar(ArrayList<Car> cs){
        System.out.print("输入需要生产车辆的数量(编号由10001开始):");
        int cnt = in.nextInt();
        for(int i = 0; i < cnt; i ++ ) {
            System.out.println("输入车辆价格,品牌,颜色,型号,生产日期(yyyy/MM/dd) : ");
            double p;
            String b, c, t, date;
            p = in.nextDouble();
            b = in.next();
            c = in.next();
            t = in.next();
            date = in.next();
            Car car = new Car(p, b, c, t, date);
            cs.add(car);
            try{
                OutputStream fos = new FileOutputStream("doc\\car.txt");
                ObjectOutputStream os = new ObjectOutputStream(fos);
                os.writeObject(car);
            }catch (IOException e){
                e.printStackTrace();
            }
//            OutputStream fos = null;
//            try {
//                fos = new FileOutputStream("doc\\car.txt");
//                fos.write(car.toString().getBytes(StandardCharsets.UTF_8));
//            } catch (IOException e) {
//                e.printStackTrace();
//            } finally {
//                if (fos != null) {
//                    try {
//                        fos.close();
//                    }catch (IOException e){
//                        e.printStackTrace();
//                    }
//                }
//            }

        }
    }

    public static void sellCar(ArrayList<Car> cs, ArrayList<CarManagement> cm){
        Car car = new Car();
        System.out.println("输入销售出的汽车的数量:");
        int cnt = in.nextInt();
        for(int i = 0; i < cnt; i ++ ){
            OutputStream fos = null;
            System.out.println("输入销售出的汽车编号, 销售单号, 买家编号:");
            int id = in.nextInt();
            int sale_id = in.nextInt();
            int cus_id = in.nextInt();
            for(Car c : cs){
                if(c.getCar_Id() == id){
                    try {
                        fos = new FileOutputStream("doc\\car_Sell.txt");
//                        fos.write(c.toString().getBytes(StandardCharsets.UTF_8));
                        ObjectOutputStream os = new ObjectOutputStream(fos);
                        os.writeObject(new CarManagement(sale_id, id, cus_id));
                    }catch (IOException e){
                        e.printStackTrace();
                    }finally {
                        if(fos != null){
                            cm.add(new CarManagement(sale_id, id, cus_id));
//                               fos.close();
                        }
                    }
                }
            }
        }
    }

    public static void listCar(ArrayList<Car> cs){
        for(Car i : cs){
            System.out.println(i.toString());
        }
    }

    public static void listSoldCar(ArrayList<CarManagement> cm){
        for(CarManagement i : cm){
            System.out.println(i.toString());
        }
    }

    public static void deleteSoldCar(ArrayList<CarManagement> cm){
        System.out.println("输入需要删除的销售数据的销售单号:");
        int id = in.nextInt();
        cm.removeIf(i -> id == i.getSales_Order());
    }
}
